package com.mvp.shq.washer.ui.shop.order.myorder;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.entity.shop.MyOrderRestult;
import com.mvp.shq.washer.entity.shop.result.MyOrderListRestult;
import com.mvp.shq.washer.entity.shop.result.OrderDataListResult;
import com.mvp.shq.washer.entity.shop.result.OrderDataResult;
import com.mvp.shq.washer.entity.shop.result.OrderDetailListRestult;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.rxbus.Subscribe;
import com.mvp.shq.washer.utils.DataDictionary;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.StringUtils;
import com.orhanobut.logger.Logger;


import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by westing on 2017/10/17.
 * 我的订单
 */
public class MyOrderActivity extends BaseActivity<Presenter, Model>  {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.vp_main)
    ViewPager vpMain;
    private Fragment[] TAB_FRAGMENTS;
    private int TAB_COUNT=0;
    private MyOrderViewPagerAdapter mAdapter;
    private int lastTabPosition = 0;
    @Override
    public int getLayoutId() {
        return R.layout.activity_myorder;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, ContextCompat.getColor(context, R.color.money), 9);
        title.setText("我的订单");
        setTabFragment();
    }

    /**
     * @description: 设置添加Tab
     */
    private void setTabs(TabLayout tabLayout, LayoutInflater inflater) {
        for (int i = 0; i < DataDictionary.ORDERSTATUETAB.length; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            View view = inflater.inflate(R.layout.category_tab_myorder, null);
            TextView tvTitle = (TextView) view.findViewById(R.id.tv_tab);
            tvTitle.setText(DataDictionary.ORDERSTATUETAB[i]);
            tab.setCustomView(view);
            tabLayout.addTab(tab);
        }

    }


    @OnClick(R.id.back)
    public void onViewClicked() {
      finish();
    }

    private void setTabFragment()
    {
        TAB_COUNT= DataDictionary.ORDERSTATUETAB.length;
        TAB_FRAGMENTS = new Fragment[TAB_COUNT];
        for (int i=0;i<TAB_COUNT;i++){
            TAB_FRAGMENTS[i] = new MyOrderListFragment(DataDictionary.ORDERSTATUELIST[i]+"");
        }
        setTabs(tablayout, this.getLayoutInflater());
        mAdapter = new MyOrderViewPagerAdapter(getSupportFragmentManager());
        vpMain.setAdapter(mAdapter);
        vpMain.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tablayout));
        tablayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(vpMain));
        tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int index = tab.getPosition();
                if (index != lastTabPosition){
                    lastTabPosition = index;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private class MyOrderViewPagerAdapter extends FragmentPagerAdapter {
        public MyOrderViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return TAB_FRAGMENTS[position];
        }

        @Override
        public int getCount() {
            return DataDictionary.ORDERSTATUETAB.length;
        }
    }

    private OrderDataListResult restult;
    @Subscribe(code = EventCode.SHOP_SAVE_ORDER_RESULT)
    public void sendOrderToComment(OrderDataListResult myOrderListRestult) {
        Logger.d("保存数据");
        restult = myOrderListRestult;
    }
    @Subscribe(code = EventCode.SHOP_SEND_ORDER_FRO_COMMENT)
    public void sendOrderToComment() {
        Logger.d("发送数据");
        RxBus.get().send(EventCode.SHOP_GET_ORDER_FRO_COMMENT,restult);
    }

}
